cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(DiaryTool CXX C)

if (MSVC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.29.30129 AND CMAKE_VERSION VERSION_GREATER 3.20.3)
    # this change happened in CMake 3.20.4
    set(CMAKE_CXX_STANDARD 23) # /std:c++latest - unlocks the non stable cpp20 features. For new 16.11 versions
else ()
    set(CMAKE_CXX_STANDARD 20) # /std:c++latest for msvc and -std=c++20 for everyone else.
endif ()

set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)

if (NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
    message(FATAL_ERROR "Only 64Bit Platform are support!!")
endif()

if (MINGW)
    set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -g)
    get_filename_component(MINGW_BIN_DIR ${CMAKE_CXX_COMPILER} DIRECTORY)

    # FreeType2
    message(STATUS "FreeType2: ${MINGW_BIN_DIR}/../include/freetype2")
    include_directories("${MINGW_BIN_DIR}/../include/freetype2")
    link_directories("${MINGW_BIN_DIR}/../lib")
    set(FT2_LIBS -lfreetype -lpng)

    set(CURL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/mingw/curl/lib/cmake/CURL)
    set(glfw3_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/mingw/glfw3/lib/cmake/glfw3)
elseif(MSVC)
    # 启用文件夹
    set_property(GLOBAL PROPERTY USE_FOLDERS ON)

    # Only Support VS2022
    if (MSVC_VERSION LESS 1930)
        message(FATAL_ERROR "Only VS2022 Support!!")
    endif()

    # Release Mode also generate PDB
    # set(CMAKE_CXX_FLAGS_RELEASE "/Zi")
    add_link_options("$<$<NOT:$<CONFIG:Debug>>:/DEBUG>")
    add_link_options("$<$<NOT:$<CONFIG:Debug>>:/OPT:REF>")
    add_link_options("$<$<NOT:$<CONFIG:Debug>>:/OPT:ICF>")

    # stb_image use unicode
    add_definitions(-DSTBI_WINDOWS_UTF8)
    add_compile_definitions($<IF:$<CONFIG:Debug>,DEBUG,NDEBUG>)

    set(CMAKE_CXX_FLAGS "/EHsc /utf-8 /MP /Zi /wd4200 /Zc:__cplusplus")
    set(CMAKE_C_FLAGS   "/EHsc /utf-8 /MP /Zi /wd4200")
#    add_compile_options(/utf-8 /MP /Zi /wd4200 /Zc:__cplusplus)
    add_definitions(
      -D_WIN32_WINNT=0x0601
      -D_CRT_SECURE_NO_WARNINGS
      -D_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING
      -D_SILENCE_ALL_CXX23_DEPRECATION_WARNINGS
    )
    add_link_options(/NODEFAULTLIB:LIBCMTD)
    
    # FreeType2
    include_directories("${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/freetype2/include")
    link_directories("${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/freetype2/lib")
    set(FT2_LIBS freetype$<$<CONFIG:Debug>:d>)

    # LibXlsWriter and ZLIB
    include_directories("${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/libxlswriter/include")
    include_directories("${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/zlib/include")
    link_directories("${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/libxlswriter/lib")
    link_directories("${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/zlib/lib")
    set(XLSWRITER_LIB xlsxwriter$<$<CONFIG:Debug>:d> zlibstatic$<$<CONFIG:Debug>:d>)

    set(ZLIB_HOME ${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/zlib)
    set(ZLIB_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/zlib)
    set(CURL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/curl/lib/cmake/CURL)
    set(glfw3_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/vs2022/glfw3/lib/cmake/glfw3)
elseif(UNIX)
    find_package(PkgConfig REQUIRED)

    add_compile_options(-fPIC -fvisibility=hidden)
    find_package(Freetype REQUIRED)
    set(FT2_LIBS Freetype::Freetype)

    option(ENABLE_DEBUG_ASAN "Debug With Asan Enabled" OFF)

    if (CMAKE_BUILD_TYPE MATCHES "Debug" AND ENABLE_DEBUG_ASAN)
      message(STATUS "Enable ASAN")
      set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-fsanitize=address -fsanitize=leak -fsanitize=undefined -fsanitize-recover=address -fno-sanitize=alignment")
      set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-fsanitize=address -fsanitize=leak -fsanitize=undefined -fsanitize-recover=address -fno-sanitize=alignment")
    endif()

    #set(ENV{PKG_CONFIG_PATH} "${CMAKE_SOURCE_DIR}/libs/linux/lib/pkgconfig")
    #pkg_search_module(XLSX-WRITER REQUIRED IMPORTED_TARGET xlsxwriter)
    #set(XLSWRITER_LIB PkgConfig::XLSX-WRITER)
    include_directories("${CMAKE_CURRENT_SOURCE_DIR}/libs/linux/include")
    link_directories("${CMAKE_CURRENT_SOURCE_DIR}/libs/linux/lib")
    set(XLSWRITER_LIB xlsxwriter -lz)
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/vendors)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/sdk/include)

find_package(CURL REQUIRED)
find_package(glfw3 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(Threads REQUIRED)

add_subdirectory(vendors)
add_subdirectory(sdk)
add_subdirectory(src)
add_subdirectory(extensions)

add_subdirectory(scripting_support)
